home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3332 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: command line argument help
  5. Date: 27 Jan 1996 15:20 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <27JAN199615205492@erich.triumf.ca>
  9. References: <4edfth$ok@muss.CIS.McMaster.CA>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4edfth$ok@muss.CIS.McMaster.CA>, shadowfax writes...
  14. >to all the c gods out there:
  15. >i am not having too much success with command line arguments.  i am 
  16. >trying to make a simple sumation executable for dos.  i am using borland 
  17. >c++ v3.1.  what i want as the end result is the user just types:
  18. >c:\> sum 6 3
  19. >and the executable would output an answer of 9.  the following is my program:
  20.  
  21. >#include <stdio.h>
  22. >#include <stdlib.h>
  23. >int main(char *argv[])
  24.  
  25. This should be:
  26.  
  27. int main(int argc, char *argv[]);
  28.  
  29. >{
  30. >   int iSum = 0;
  31.  
  32. You should check the value of argc here, to ensure that the user has entered
  33. enough values, and that you won't try to read more than he has entered.
  34.  
  35. >   iSum = atoi(argv[1]) + atoi(argv[2]);
  36. >   printf("\nthe answer is %d", iSum);
  37.  
  38. >   return(0);
  39. >}
  40. >when i compile and run, it always outputs an anser of 0.  can anyone tell 
  41. >me what's wrong?
  42.  
  43. Since the first argument of main() is a count of the number of arguments, and
  44. your program treated it as a char * to the arguments, anything could happen!
  45.  
  46. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  47. Internet: bennett@triumf.ca         | of one another only when one can be
  48. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  49. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  50. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.